package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.SystemUserOrganization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2018/12/3 17:05
 * @Description:
 */
@Repository("systemUserOrganizationDao")
public class SystemUserOrganizationDao implements CrudDao<SystemUserOrganization> {

    private final static Logger logger = LoggerFactory.getLogger(SystemUserOrganizationDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        for(String key:queryHash.keySet()){
            String value = queryHash.get(key);
            switch (key){
                case "enterpriseId":
                    conditionHash.put("s.organization.enterprise.id = ?{paramIndex}", StringConverters.null2Long(value));
                    continue;
                case "personnelId":
                    conditionHash.put("s.personnel.id = ?{paramIndex}", StringConverters.null2Long(value));
                    continue;
                case "systemUserId":
                    conditionHash.put("s.systemUser.id = ?{paramIndex}",StringConverters.null2Long(value));
                    continue;
                case "organizationId":
                    conditionHash.put("s.organization.id = ?{paramIndex}", StringConverters.null2Long(value));
                    continue;

            }
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {

        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from SystemUserOrganization s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<SystemUserOrganization> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from SystemUserOrganization s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<SystemUserOrganization> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, SystemUserOrganization.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public SystemUserOrganization findModel(Long userId) {
        return entityManager.find(SystemUserOrganization.class, userId);
    }

    @Override
    public Integer add(SystemUserOrganization systemUserOrganization) {
        systemUserOrganization.setCreateDate(new Date());
        entityManager.persist(systemUserOrganization);
        logger.info("SystemUserOrganizationDaoImpl添加systemUserOrganization成功！");
        return 1;
    }

    @Override
    public Integer update(SystemUserOrganization systemUserOrganization) {
        systemUserOrganization.setUpdateDate(new Date());
        entityManager.merge(systemUserOrganization);
        return 1;
    }

    @Override
    public Integer delete(Long systemUserOrganizationId) {
        SystemUserOrganization systemUserOrganization = entityManager.find(SystemUserOrganization.class, systemUserOrganizationId);
        entityManager.remove(systemUserOrganization);
        return 1;
    }

    @Override
    public Integer deleteLogic(SystemUserOrganization model) {
        model.setDeleteStatus(true);
        return 1;
    }

    public List<SystemUserOrganization> hqlExcute(String hql) {
        return entityManager.createQuery(hql,SystemUserOrganization.class).getResultList();
    }
}
